{include file="common/header" /}

<div class="layui-container" style="margin-top: 15px;">

    <div class="layui-input-inline">
        <input type="text" id="search_name" name="student_name" placeholder="请输入学员姓名" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-input-inline">
        <button class="layui-btn" id="search">
            <i class="layui-icon layui-icon-search"></i>筛选</button>
        <button class="layui-btn layui-btn-danger" id="clear">
            <i class="layui-icon"></i>重置</button>
    </div>


    <div id="root"  style="margin-left: -15px;"></div>
    <div class="layui-form-item" style="text-align: right; margin-right: -25px;  margin-top: 10px">
        <button class="layui-btn layui-btn-normal"  id="sure">确定</button>
        <button class="layui-btn  layui-btn-primary"  id="cancel">取消</button>
    </div>

</div>

<!--底部公共部分-->
{include file="common/footer" /}
<script>


    var datajson= eval('{$jsonmsg}');  //返回数组
    var room_id = '{$room_id}';  //教室id字符串
    var class_id = '{$class_id}'; //班级ID
    var campus_id = '{$campus_id}'; //班级ID
    var data_Left = [];  //初始左边值
    var data_Rigth = [];  //初始右边边值
    console.log(datajson)
    for(var j =0; j < datajson.length; j ++){
        if((datajson[j].status_match) === 1) {  //包含
            var row = {};
            row.course_elimination = datajson[j].course_elimination;
            row.course_elimination_give = datajson[j].course_elimination_give;
            row.id = datajson[j].id;
            row.course = datajson[j].course;
            row.campus = datajson[j].campus;
            row.student_name = datajson[j].student_name;
            row.status_text = datajson[j].status_text;
            row.course_name = datajson[j].course_name;
            row.contract_number = datajson[j].contract_number;
            row.type = datajson[j].type;
            data_Rigth.push(row);
        }else if((datajson[j].status_match) === 0){  //不包含

            var row = {};
            row.course_elimination = datajson[j].course_elimination;
            row.course_elimination_give = datajson[j].course_elimination_give;
            row.id = datajson[j].id;
            row.course = datajson[j].course;
            row.campus = datajson[j].campus;
            row.student_name = datajson[j].student_name;
            row.status_text = datajson[j].status_text;
            row.course_name = datajson[j].course_name;
            row.contract_number = datajson[j].contract_number;
            row.type = datajson[j].type;
            data_Left.push(row);
        }
    }

    layui.config({
        base: '/static/layui_exts/transfer/'
    }).use(['element',  'table', 'jquery','form','layer','transfer'],function () {

        var transfer = layui.transfer,$ = layui.$,table = layui.tabl;



        /*
        var $ = layui.jquery
            ,element = layui.element
            ,layer = layui.layer
            ,transfer = layui.transfer
            ,form = layui.form;
            */
        //表格列
        var cols = [{type: 'checkbox', fixed: 'left'},
            {field: 'student_name', title: '学员姓名',style:'color:#01AAED; font-size:12px; font-weight: bold;', align: 'center', width: 125, sort: true},
            {field: 'course_name',align: 'center',  style:'font-size:12px; font-weight: bold;', width: 300,title: '报名项目'},
            {field: 'course_elimination',align: 'center', style:'font-size:12px; color:#ef4136;font-weight: bold;', width: 100,title: '剩余课时'},
            {field: 'course_elimination_give',align: 'center', style:'font-size:12px; color:#ef4136;font-weight: bold;', width: 150,title: '赠送剩余课时'},
            {field: 'contract_number',align: 'center', style:'font-size:12px; font-weight: bold;', width:200,title: '订单'},
            {field: 'status_text',align: 'center',style:'font-size:12px; font-weight: bold;', width: 100, title: '付款状态'}
            ]
        //表格配置文件
        var tabConfig = {'page':true,'limits':[10,50,100],'height':400, 'width':500}
        var tableIns = transfer.render({
            elem: "#root", //指定元素
            cols: cols, //表格列  支持layui数据表格所有配置
            data: [data_Left,data_Rigth], //[左表数据,右表数据[非必填]]
            tabConfig: tabConfig, //表格配置项 支持layui数据表格所有配置
        })

        //搜索项
        $('#search').click(function () {
            var left_data = transfer.get(tableIns,'left','id'); //左边的数据
            var right_data = transfer.get(tableIns,'right','id'); //右边边的数据
            var left_arr = [];
            var rigth_arr = [];
            //左边
            for(var j =0; j < datajson.length; j ++){
                if(left_data.indexOf(datajson[j].id) != -1) {
                    var row = {};
                    row.course_elimination = datajson[j].course_elimination;
                    row.course_elimination_give = datajson[j].course_elimination_give;
                    row.id = datajson[j].id;
                    row.course = datajson[j].course;
                    row.campus = datajson[j].campus;
                    row.student_name = datajson[j].student_name;
                    row.status_text = datajson[j].status_text;
                    row.course_name = datajson[j].course_name;
                    row.contract_number = datajson[j].contract_number;
                    row.type = datajson[j].type;
                    left_arr.push(row);
                }
            }

			
            //右边
            for(var j =0; j < datajson.length; j ++){
                if(right_data.indexOf(datajson[j].id) != -1) {
                    var row = {};
                    row.course_elimination = datajson[j].course_elimination;
                    row.course_elimination_give = datajson[j].course_elimination_give;
                    row.id = datajson[j].id;
                    row.course = datajson[j].course;
                    row.campus = datajson[j].campus;
                    row.student_name = datajson[j].student_name;
                    row.status_text = datajson[j].status_text;
                    row.course_name = datajson[j].course_name;
                    row.contract_number = datajson[j].contract_number;
                    row.type = datajson[j].type;
                    rigth_arr.push(row);
                }
            }
			
            var search_arr = [];
            var student_name = $('input[name=student_name]').val();
            console.log(student_name)
            for(var j =0; j < left_arr.length; j ++){
					var student_name_ = left_arr[j].student_name + '';
                if(student_name_.indexOf(student_name + '') != -1){
                    var row = {};
                    row.course_elimination = left_arr[j ].course_elimination;
                    row.course_elimination_give = left_arr[j ].course_elimination_give;
                    row.id = left_arr[j].id;
                    row.course = left_arr[j].course;
                    row.campus = left_arr[j].campus;
                    row.student_name = left_arr[j].student_name;
                    row.status_text = left_arr[j].status_text;
                    row.course_name = left_arr[j].course_name;
                    row.contract_number = left_arr[j].contract_number;
                    row.type = left_arr[j].type;
                    search_arr.push(row);
                }

            }

            tableIns = transfer.render({
                elem: "#root", //指定元素
                id: 'studentReload',
                cols: cols, //表格列  支持layui数据表格所有配置
                data: [search_arr,rigth_arr], //[左表数据,右表数据[非必填]]
                tabConfig: tabConfig //表格配置项 支持layui数据表格所有配置

            })
			

        });

        //重置
        $('#clear').click(function () {
            var right_data = transfer.get(tableIns,'right','id'); //右边边的数据
            var left_arr = [];
            var rigth_arr = [];
            //右边
            for(var j =0; j < datajson.length; j ++){
                if(right_data.indexOf(datajson[j].id) != -1) {
                    var row = {};
                    row.course_elimination = datajson[j].course_elimination;
                    row.course_elimination_give = datajson[j].course_elimination_give;
                    row.id = datajson[j].id;
                    row.course = datajson[j].course;
                    row.campus = datajson[j].campus;
                    row.student_name = datajson[j].student_name;
                    row.status_text = datajson[j].status_text;
                    row.course_name = datajson[j].course_name;
                    row.contract_number = datajson[j].contract_number;
                    row.type = datajson[j].type;
                    rigth_arr.push(row);
                }else{
                    var row = {};
                    row.course_elimination = datajson[j].course_elimination;
                    row.course_elimination_give = datajson[j].course_elimination_give;
                    row.id = datajson[j].id;
                    row.course = datajson[j].course;
                    row.campus = datajson[j].campus;
                    row.student_name = datajson[j].student_name;
                    row.status_text = datajson[j].status_text;
                    row.course_name = datajson[j].course_name;
                    row.contract_number = datajson[j].contract_number;
                    row.type = datajson[j].type;
                    left_arr.push(row);
                }
            }

         transfer.render({
                elem: "#root", //指定元素
                id: 'studentReload',
                cols: cols, //表格列  支持layui数据表格所有配置
                data: [left_arr,rigth_arr], //[左表数据,右表数据[非必填]]
                tabConfig: tabConfig //表格配置项 支持layui数据表格所有配置
            })
            $('input[name=student_name]').val("");

        });


       // transfer.get(参数1:初始化返回值,参数2:获取数据[all,left,right,l,r],参数:指定数据字段)
        //获取数据
        $('.all').on('click',function () {
            var data = transfer.get(tableIns,'all');
            layer.msg(JSON.stringify(data))
        });
        $('.left').on('click',function () {
            var data = transfer.get(tableIns,'left','id');
            layer.msg(JSON.stringify(data))
        });
        $('.right').on('click',function () {
            var data = transfer.get(tableIns,'right','id');
            layer.msg(JSON.stringify(data))
        });


        //监听提交
        $('#sure').on('click', function (data) {
         //   var field = data.field;
            var isLoad;

            /***右边**/
            var data_str_right = transfer.get(tableIns,'right','id');
            var right_id_arr = (data_str_right+"").split(",");
            var choice_arr_right = [];

            for(var j =0; j < datajson.length; j ++){
                if((datajson[j].status_match) == 0  && isInArray(right_id_arr,datajson[j].id)) {  //包含
                    var row = {};
                    row.course_elimination = datajson[j].course_elimination;
                    row.course_elimination_give = datajson[j].course_elimination_give;
                    row.id = datajson[j].id;
                    row.course = datajson[j].course;
                    row.campus = datajson[j].campus;
                    row.student_name = datajson[j].student_name;
                    row.status_match = datajson[j].status_match;
                    row.student_id = datajson[j].student_id;
                    row.status_text = datajson[j].status_text;
                    row.course_name = datajson[j].course_name;
                    row.contract_number = datajson[j].contract_number;
                    row.type = datajson[j].type;
                    choice_arr_right.push(row);
                }

             }

            /**左边**/
            var data_str_left = transfer.get(tableIns,'left','id');
            var left_id_arr = (data_str_left+"").split(",");
            var choice_arr_left = [];

            for(var j =0; j < datajson.length; j ++){
                if((datajson[j].status_match) == 1  && isInArray(left_id_arr,datajson[j].id)) {  //包含
                    var row = {};
                    row.course_elimination = datajson[j].course_elimination;
                    row.course_elimination_give = datajson[j].course_elimination_give;
                    row.id = datajson[j].id;
                    row.course = datajson[j].course;
                    row.campus = datajson[j].campus;
                    row.student_name = datajson[j].student_name;
                    row.status_match = datajson[j].status_match;
                    row.student_id = datajson[j].student_id;
                    row.status_text = datajson[j].status_text;
                    row.course_name = datajson[j].course_name;
                    row.contract_number = datajson[j].contract_number;
                    row.type = datajson[j].type;
                    choice_arr_left.push(row);
                }

            }





            var field = {};
            var studentdata= JSON.stringify(choice_arr_right);
            var studentdata_delete= JSON.stringify(choice_arr_left);
            field.studentdata = studentdata; //json字符串
            field.studentdata_delete = studentdata_delete; //json字符串
            field.room_id = room_id;  //教室字符串
            field.class_id = class_id;//班级ID
            field.campus_id = campus_id;//班级ID
            $.ajax({
                url: '/admin/grades/insert_student_table',
                method: "post",
                data: field,
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    }); //0代表加载的风格，支持0-2
                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 1) {

                        setTimeout(function () {

                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            parent.location.reload();

                        }, 1000);


                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });

            return false;
        });


        $('#cancel').on('click', function (data) {

            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭

            return false;
        });


        function isInArray(arr,value){
            for(var i = 0; i < arr.length; i++){
                if(value == arr[i]){
                    return true;
                }
            }
            return false;
        }
    })




</script>
